<?php
include_once("ConnectionManager.php");

function countAllProduct($keyword){
    $query = "SELECT COUNT(*) AS NUM 
              FROM MITI_PRODUCT
              WHERE (PRO_CODE LIKE '%".htmlspecialchars($keyword, ENT_QUOTES)."%' OR 
                     PRO_NAME LIKE '%".htmlspecialchars($keyword, ENT_QUOTES)."%')";
    ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row["NUM"];
}

function getTotalProduct(){
    $query = "SELECT COUNT(PRO_ID) AS QTY FROM MITI_PRODUCT";
    ConnectionManager::getInstance()->setQuery($query);
    $array = ConnectionManager::getInstance()->query();
    $result = mysql_fetch_array($array);
    return $result["QTY"];
}

function getActiveTotalProduct(){
    $query = "SELECT COUNT(PRO_ID) AS QTY FROM MITI_PRODUCT WHERE PRO_STATUS = 1";
    ConnectionManager::getInstance()->setQuery($query);
    $array = ConnectionManager::getInstance()->query();
    $result = mysql_fetch_array($array);
    return $result["QTY"];
}

function getDiscountProduct(){
    $query = "SELECT COUNT(PRO_ID) AS QTY FROM MITI_PRODUCT WHERE DIS_ID IS NOT NULL";
    ConnectionManager::getInstance()->setQuery($query);
    $array = ConnectionManager::getInstance()->query();
    $result = mysql_fetch_array($array);
    return $result["QTY"];
}

function getBestSellerProduct(){
    $query = "select * from MITI_PRODUCT where PRO_ID in (select distinct PRO_ID from MITI_RECEIPT_ITEM where REC_ID in (select REC_ID from MITI_RECEIPT where DATE(REC_DATE) = CURDATE()))";
    ConnectionManager::getInstance()->setQuery($query);
    $bestsellers  = ConnectionManager::getInstance()->query();
    $output = array();
    if(mysql_num_rows($bestsellers) < 21){
        $missQty = 21 - mysql_num_rows($bestsellers); 
        $query = "select * from MITI_PRODUCT order by PRO_ID DESC LIMIT 0,$missQty";
        ConnectionManager::getInstance()->setQuery($query);
        $addition  = ConnectionManager::getInstance()->query();
        
        while($item = mysql_fetch_array($bestsellers)){
            array_push($output,$item);
        }
        while($itema = mysql_fetch_array($addition)){
            array_push($output,$itema);
        }
    }else{
        while($item = mysql_fetch_array($bestsellers)){
            array_push($output,$item);
        }
    }
    return $output;
}   

function getLatestProduct($start_record, $qty){
    $start_record = $start_record -1;
    $query = "SELECT PRO_ID, PRO_CODE, PRO_NAME, PRO_STATUS, PRO_PRICE, DIS_ID, PRO_STAT_ID, CAT_ID FROM MITI_PRODUCT ORDER BY PRO_ID DESC LIMIT $start_record,$qty";   
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function getProductOfCategory($cat_id,$start_record, $qty){
    $start_record = $start_record -1;
    if(is_null($qty)){
        $query = "SELECT PRO_ID, PRO_CODE, PRO_NAME, PRO_STATUS, PRO_PRICE, DIS_ID, PRO_STAT_ID FROM MITI_PRODUCT WHERE CAT_ID = $cat_id ORDER BY PRO_NAME";    
    }else{
        $query = "SELECT PRO_ID, PRO_CODE, PRO_NAME, PRO_STATUS, PRO_PRICE, DIS_ID, PRO_STAT_ID FROM MITI_PRODUCT WHERE CAT_ID = $cat_id ORDER BY PRO_NAME LIMIT $start_record,$qty";    
    }
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function searchKeyword($keyword, $startrecord, $qty){
    $startrecord = $startrecord -1;
    if(is_null($qty)){
        $query = "SELECT PRO_ID, PRO_CODE, PRO_NAME, PRO_STATUS, PRO_PRICE, DIS_ID, PRO_STAT_ID FROM MITI_PRODUCT WHERE PRO_CODE LIKE '%".urldecode($keyword)."%' OR PRO_NAME LIKE '%".urldecode($keyword)."%' ORDER BY PRO_NAME";    
    }else{
        $query = "SELECT PRO_ID, PRO_CODE, PRO_NAME, PRO_STATUS, PRO_PRICE, DIS_ID, PRO_STAT_ID FROM MITI_PRODUCT WHERE PRO_CODE LIKE '%".urldecode($keyword)."%' OR PRO_NAME LIKE '%".urldecode($keyword)."%' ORDER BY PRO_NAME LIMIT $startrecord,$qty";
    }
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function getAllProducts(){
    $query = "SELECT PRO_ID, PRO_CODE, PRO_NAME, PRO_STATUS, PRO_PRICE, DIS_ID 
              FROM MITI_PRODUCT";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function getProduct($pro_id){
    $query = "SELECT PRO_ID, PRO_CODE, PRO_NAME, PRO_STATUS, PRO_PRICE, DIS_ID, PRO_DESCRIPTION, CAT_ID FROM MITI_PRODUCT WHERE PRO_ID = $pro_id";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    if(mysql_num_rows($result) > 0){
        return mysql_fetch_array($result);
    }
    else{
        return null;
    }   
}

function updateProductPromotion($proId , $promotionId){
    $query = null;
    if($promotionId != "") 
        $query = "UPDATE MITI_PRODUCT SET DIS_ID = $promotionId WHERE PRO_ID = $proId";
    else
        $query = "UPDATE MITI_PRODUCT SET DIS_ID = NULL WHERE PRO_ID = $proId";
        
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    if(mysql_affected_rows() > 0) return true;
    else return false;
}

function updateProductPrice($id, $price){
    $query = "UPDATE MITI_PRODUCT SET PRO_PRICE = $price WHERE PRO_ID = $id";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    
    if(mysql_affected_rows() > 0) return true;
    else return false;
}

function createProduct($disId, $catId, $proStatid, $code, $name, $status, $price, $description){
    if($disId != "")
        $query = "INSERT INTO MITI_PRODUCT(DIS_ID, CAT_ID, PRO_STAT_ID, PRO_CODE, PRO_NAME, PRO_STATUS, PRO_PRICE, PRO_DESCRIPTION) VALUES
                                     ($disId, $catId, $proStatid, '$code', '$name', $status, $price, '$description')";
    else
        $query = "INSERT INTO MITI_PRODUCT(DIS_ID, CAT_ID, PRO_STAT_ID, PRO_CODE, PRO_NAME, PRO_STATUS, PRO_PRICE, PRO_DESCRIPTION) VALUES
                                     (NULL, $catId, $proStatid, '$code', '$name', $status, $price, '$description')";    
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_insert_id();
}

function updateProduct($disId, $proStatId, $code, $name, $status, $price, $description, $proId){
    if($disId != "")
        $query = "UPDATE MITI_PRODUCT SET DIS_ID = $disId, PRO_STAT_ID = $proStatId, PRO_CODE = '$code',PRO_NAME = '$name', 
                                 PRO_PRICE = $price, PRO_DESCRIPTION = '$description' WHERE PRO_ID = $proId";    
    else
        $query = "UPDATE MITI_PRODUCT SET DIS_ID = NULL, PRO_STAT_ID = $proStatId, PRO_CODE = '$code',PRO_NAME = '$name', 
                                 PRO_PRICE = $price, PRO_DESCRIPTION = '$description' WHERE PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    if(mysql_affected_rows() > 0) return true;
    else return false;
}

function isUniqueCode($code){
    $query = "SELECT COUNT(*) AS COUNT FROM MITI_PRODUCT WHERE PRO_CODE = '$code'";
    ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    
    return $row["COUNT"];
}

function findProductBy($proId){
    $query = "SELECT (CASE PRO_STATUS 
                      WHEN 1 THEN 'Mới' 
                      WHEN 2 THEN 'Hàng sắp về'
                      WHEN 3 THEN 'HẾT HÀNG'
                      WHEN 4 THEN 'Ẩn'
                      ELSE 'Không' END) AS STATUS, 
                     (SELECT CAT_NAME FROM MITI_CATEGORY C WHERE C.CAT_ID = P.CAT_ID) AS CATEGORY,
                     (SELECT DIS_NAME FROM MITI_DISCOUNT D WHERE D.DIS_ID = P.DIS_ID) AS DISCOUNT,
                      PRO_ID, DIS_ID, CAT_ID, PRO_STAT_ID, PRO_CODE, PRO_NAME, PRO_STATUS, PRO_PRICE, PRO_DESCRIPTION FROM MITI_PRODUCT P WHERE P.PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row;
}

function findImagesOfProduct($proId){
    $query = "SELECT IMG_ID, IMG_PATH FROM MITI_IMAGE WHERE PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function findLimitedImagesOfProduct($proId,$limit){
    $query = "SELECT IMG_ID, IMG_PATH FROM MITI_IMAGE WHERE PRO_ID = $proId LIMIT 0,$limit";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function updateProductStatus($proId, $status){
    $query = "UPDATE MITI_PRODUCT SET PRO_STATUS = $status WHERE PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows() > 0;
}

function isDeletable($proId){
    $query = "SELECT COUNT(*) AS COUNT 
              FROM MITI_PRODUCT P 
              WHERE ((SELECT COUNT(*) FROM MITI_ATTRIBUTE_VALUE AV WHERE AV.PRO_ID = P.PRO_ID) > 0 ||
                     (SELECT COUNT(*) FROM MITI_OPTIONAL_VALUE OV WHERE OV.PRO_ID = P.PRO_ID) > 0 || 
                     (SELECT COUNT(*) FROM MITI_RECEIPT_ITEM RI WHERE RI.PRO_ID = P.PRO_ID) > 0  ||
			         (SELECT COUNT(*) FROM MITI_BANNER BA WHERE BA.BANNER_PRO_ID = P.PRO_ID) > 0) AND P.PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row["COUNT"] > 0;
}

function deleteProduct($proId){
	$query = "DELETE FROM MITI_PRODUCT WHERE PRO_ID = $proId";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows() > 0;
}

function unlikeProduct($proid,$userid){
    $query = "DELETE FROM MITI_FAVOURITE WHERE PRO_ID = $proid AND USER_ID = $userid";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows();
}

function likeProduct($proid,$userid){
    $query = "INSERT INTO MITI_FAVOURITE VALUES($proid,$userid)";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows();
}

function isLikeByUser($proid, $userid){
    $query = "SELECT COUNT(PRO_ID) AS QTY FROM MITI_FAVOURITE WHERE PRO_ID = $proid AND USER_ID = $userid";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    $row = mysql_fetch_array($result);
    return $row["QTY"] > 0;
}

function getTotalLike($userid){
    $query = "SELECT COUNT(PRO_ID) AS QTY FROM MITI_FAVOURITE WHERE USER_ID = $userid";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    $row = mysql_fetch_array($result);
    return $row["QTY"];
}

function getLikeOfUser($userid){
    $query = "SELECT (SELECT PRO_PRICE FROM MITI_PRODUCT WHERE PRO_ID = F.PRO_ID) AS PRO_PRICE,PRO_ID,(SELECT PRO_NAME FROM MITI_PRODUCT WHERE PRO_ID = F.PRO_ID) AS PRO_NAME FROM MITI_FAVOURITE F WHERE USER_ID = $userid";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function getNumberBuyer($proid){
    $query = "SELECT COUNT(PRO_ID) AS QTY FROM MITI_RECEIPT_ITEM WHERE PRO_ID = $proid";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    $row = mysql_fetch_array($result);
    return $row["QTY"];
}

function getDiscountedAndOptionalPriceOfProduct($proId){
	if($proId == "") $proId = 0;
    $query = "SELECT P.PRO_PRICE - (P.PRO_PRICE * IFNULL((SELECT DIS_VALUE FROM MITI_DISCOUNT D WHERE D.DIS_ID = P.DIS_ID), 0)) + 
                     IFNULL((SELECT SUM(OV.OPV_VALUE) FROM MITI_OPTIONAL_VALUE OV WHERE OV.PRO_ID = P.PRO_ID), 0) AS PRICE
              FROM MITI_PRODUCT P 
              WHERE P.PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row["PRICE"];
}

function findProductNotLinkToSpecialProductBy($catId, $proId){
    $query = "SELECT P.PRO_ID, P.PRO_NAME, P.PRO_CODE 
              FROM MITI_PRODUCT P 
              WHERE CAT_ID = $catId AND P.PRO_ID NOT IN (SELECT SPRO_PRO_ID FROM MITI_SPECIAL_PRODUCT) 
              UNION 
              SELECT P.PRO_ID, P.PRO_NAME, P.PRO_CODE FROM MITI_PRODUCT P WHERE CAT_ID = $catId AND P.PRO_ID = $proId 
              ORDER BY PRO_NAME";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function findProductNotLinkToNewProductBannerBy($catId, $proId){
	$query = "SELECT P.PRO_ID, P.PRO_NAME, P.PRO_CODE 
              FROM MITI_PRODUCT P 
              WHERE CAT_ID = $catId AND P.PRO_ID NOT IN (SELECT BANNER_PRO_ID FROM MITI_BANNER WHERE BANNER_TYPE = 4) 
              UNION 
              SELECT P.PRO_ID, P.PRO_NAME, P.PRO_CODE FROM MITI_PRODUCT P WHERE CAT_ID = $catId AND P.PRO_ID = $proId 
              ORDER BY PRO_NAME";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}
//------------------------------ Attribute_Value ------------------------------//

function createAttributeValue($catId, $attId, $proId, $order, $value){
    $query = "INSERT INTO MITI_ATTRIBUTE_VALUE VALUES ($catId, $attId, $proId, $order, '$value')";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows();
}

function updateAttributeValue($catId, $attId, $proId, $order, $value){
    $query = "UPDATE MITI_ATTRIBUTE_VALUE SET AVAL_ORDER = $order, AVAL_VALUE = '$value' 
                    WHERE CAT_ID = $catId AND ATT_ID = $attId AND PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    if(mysql_affected_rows() > 0) return true;
    else return false;
}

function findAttributeValueBy($catId, $attId, $proId){
    $query = "SELECT AVAL_VALUE, AVAL_ORDER FROM MITI_ATTRIBUTE_VALUE WHERE CAT_ID = $catId AND ATT_ID = $attId AND PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row;
}

function deleteAttributeValueBy($catId, $proId){
    $query = "DELETE FROM MITI_ATTRIBUTE_VALUE WHERE CAT_ID = $catId AND PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

//------------------------------ Option_Value ------------------------------//
function createOptionalValue($catId, $oppId, $proId, $value){
    $query = "INSERT INTO MITI_OPTIONAL_VALUE VALUES($catId, $oppId, $proId, $value)";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows();
}

function updateOptionalValue($catId, $oppId, $proId, $value){
    $query = "UPDATE MITI_OPTIONAL_VALUE SET OPV_VALUE = $value WHERE CAT_ID = $catId AND OPP_ID = $oppId AND PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    if(mysql_affected_rows() > 0) return true;
    else return false;
}

function findOptionalPriceBy($catId, $oppId, $proId){
    $query = "SELECT OPV_VALUE FROM MITI_OPTIONAL_VALUE WHERE CAT_ID = $catId AND OPP_ID = $oppId AND PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row;
}

function deleteOptionalValueBy($catId, $proId){
    $query = "DELETE FROM MITI_OPTIONAL_VALUE WHERE CAT_ID = $catId and PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

// to insert to receipt item note
function findOptionalPriceNoteBy($proId){
    $query = "SELECT (SELECT OPP_NAME FROM MITI_OPTIONAL_PRICE OP WHERE OP.OPP_ID = OV.OPP_ID) AS NAME, OV.OPV_VALUE
              FROM MITI_OPTIONAL_VALUE OV 
              WHERE OV.PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

//------------------------------ DISCOUNT ------------------------------//
function getAllDiscounts(){
    $query = "SELECT DIS_ID, DIS_NAME, DIS_VALUE FROM MITI_DISCOUNT";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}
function getDiscount($discount_id){
    $query = "SELECT DIS_ID, DIS_NAME,DIS_VALUE, DIS_LOGO FROM MITI_DISCOUNT WHERE DIS_ID = $discount_id";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    if(mysql_num_rows($result)>0){
        $discount = mysql_fetch_array($result);
        return $discount;    
    }else{
        return null;
    }
}
function updateDiscount($disId, $name, $value){
	$query = "UPDATE MITI_DISCOUNT SET DIS_NAME = '$name', DIS_VALUE = $value WHERE DIS_ID = $disId";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    
    return mysql_affected_rows() > 0;
}

function createDiscount($name, $image, $value){
	$query = null;
	if($image == "") 
		$query = "INSERT INTO MITI_DISCOUNT(DIS_NAME, DIS_LOGO, DIS_VALUE) VALUES('$name', NULL ,$value)";
	else
		$query = "INSERT INTO MITI_DISCOUNT(DIS_NAME, DIS_LOGO, DIS_VALUE) VALUES('$name', '$image' ,$value)";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_insert_id();
}

function isDeletableDiscount($disId){
	$query = "SELECT COUNT(*) AS COUNT FROM MITI_PRODUCT WHERE DIS_ID = $disId";
	ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row["COUNT"] > 0;
}

function findDiscountBy($disId){
    if($disId == "") $disId = 0;
	$query = "SELECT DIS_ID, DIS_NAME, DIS_VALUE FROM MITI_DISCOUNT WHERE DIS_ID = $disId";
	ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row;
}

function deleteDiscount($disId){
	$query = "DELETE FROM MITI_DISCOUNT WHERE DIS_ID = $disId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows() > 0;
}

function updateDiscountImage($disId, $image){
	$query = null;
	if($image == "")
		$query = "UPDATE MITI_DISCOUNT SET DIS_LOGO = NULL WHERE DIS_ID = $disId";
	else 
		$query = "UPDATE MITI_DISCOUNT SET DIS_LOGO = '$image' WHERE DIS_ID = $disId";
	ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
	return mysql_affected_rows() > 0;
}

//------------------------------ IMAGE ------------------------------//
// create image for normal MITI_PRODUCT
function createNormalImage($proId, $path){
    $query = "INSERT INTO MITI_IMAGE(PRO_ID, IMG_PATH) VALUES ($proId, '$path')"; 
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_insert_id();
}

function deleteImageOfNormalProduct($imagePath, $proId){
    $query = "DELETE FROM MITI_IMAGE WHERE IMG_PATH = '$imagePath' and PRO_ID = $proId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

//------------------------------ PRODUCT STATUS ------------------------------//
function createProductStatus($name, $image){
    $query = "INSERT INTO MITI_PRODUCT_STATUS(PRO_STAT_NAME, PRO_STAT_LOGO) VALUES ('$name', '$image')";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_insert_id();
}

function findAllProductStatuses(){
    $query = "SELECT * FROM MITI_PRODUCT_STATUS";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function findProductStatusBy($proStatId){
    $query = "SELECT * FROM MITI_PRODUCT_STATUS WHERE PRO_STAT_ID = $proStatId";
    ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row;
}

// update external product status
function updateStatusOfProduct($proStatId, $name){
    $query = "UPDATE MITI_PRODUCT_STATUS SET PRO_STAT_NAME = '$name' WHERE PRO_STAT_ID = $proStatId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows() > 0;
}

function updateImageOfProductStatus($proStatId, $image){
    $query = "UPDATE MITI_PRODUCT_STATUS SET PRO_STAT_LOGO = '$image' WHERE PRO_STAT_ID = $pro$proStatId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows() > 0;
}

function isDeletableProductStatus($proStatId){
    $query = "SELECT COUNT(*) AS COUNT FROM MITI_PRODUCT WHERE PRO_STAT_ID = $proStatId";
    ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row["COUNT"] > 0; 
}

function deleteProductStatus($proStatId){
    $query = "DELETE FROM MITI_PRODUCT_STATUS WHERE PRO_STAT_ID = $proStatId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows() > 0;
}
?>
